package com.kakarote.hrm.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import com.kakarote.core.servlet.BaseServiceImpl;
import com.kakarote.hrm.entity.PO.HrmDept;
import com.kakarote.hrm.entity.PO.HrmEmployeeConcurrentPost;
import com.kakarote.hrm.mapper.HrmEmployeeConcurrentPostMapper;
import com.kakarote.hrm.service.IHrmDeptService;
import com.kakarote.hrm.service.IHrmEmployeeConcurrentPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 兼职信息 服务实现类
 * </p>
 *
 * @author so11
 * @since 2024-04-13
 */
@Service
public class HrmEmployeeConcurrentPostServiceImpl extends BaseServiceImpl<HrmEmployeeConcurrentPostMapper, HrmEmployeeConcurrentPost> implements IHrmEmployeeConcurrentPostService {


    @Autowired
    private HrmEmployeeConcurrentPostMapper hrmEmployeeConcurrentPostMapper;
    @Autowired
    private IHrmDeptService hrmDeptService;

    @Override
    public List<Map<String, Object>> queryEmployeeConcurrentPostListMap(List<Long> employeeIds) {
        List<Map<String, Object>> maps = hrmEmployeeConcurrentPostMapper.queryEmployeeConcurrentPostListMap(employeeIds);
        if (maps != null) {
            List<HrmDept> deptList = hrmDeptService.lambdaQuery().list();
            Map<Long, HrmDept> deptMap = deptList.stream().collect(Collectors.toMap(HrmDept::getDeptId, Function.identity()));

            for (Map<String, Object> map : maps) {
                if (map.get("companyAgeStartTime") != null && StrUtil.isNotBlank(map.get("companyAgeStartTime").toString())){
                    LocalDateTime companyAgeStartTime = LocalDateTimeUtil.parse(map.get("companyAgeStartTime").toString(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                    if (companyAgeStartTime != null){
                        long nowCompanyAge = LocalDateTimeUtil.between(companyAgeStartTime, LocalDateTime.now()).toDays() + 1;
                        if (LocalDateTimeUtil.toEpochMilli(companyAgeStartTime) > System.currentTimeMillis()) {
                            nowCompanyAge = 0;
                        }
                        map.put("companyAge", String.valueOf(nowCompanyAge));
                    }
                }
                Object deptIdObj = map.get("deptId");
                if (deptIdObj != null && StrUtil.isNotBlank(deptIdObj.toString())) {
                    Long deptId = Long.parseLong(deptIdObj.toString());
                    HrmDept hrmDept = deptMap.get(deptId);
                    if (hrmDept != null) {
                        if (hrmDept.getDeptType() == 2) {
                            map.put("deptName",hrmDept.getName());
                            HrmDept parentDept = deptMap.get(hrmDept.getParentId());
                            if (parentDept != null) {
                                map.put("companyName",parentDept.getName());
                            }
                        } else if (hrmDept.getDeptType() == 1) {
                            map.put("companyName",hrmDept.getName());
                        }
                    }
                }
            }
        }
        return maps;
    }
}
